package com.phiture.erp.produce.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.phiture.erp.basic.api.pojo.resp.ErpMaterialInfoResp;
import com.phiture.erp.basic.api.pojo.resp.ErpMaterialPlanResp;
import com.phiture.erp.basic.api.service.ErpMaterialApi;
import com.phiture.erp.basic.api.service.ErpMaterialPlanApi;
import com.phiture.erp.produce.core.convert.ErpBomConvertMapper;
import com.phiture.erp.produce.core.pojo.dto.MrpBomInfoDTO;
import com.phiture.erp.produce.core.service.BomService;
import com.phiture.erp.produce.dal.entity.ErpBomDO;
import com.phiture.erp.produce.dal.entity.ErpBomItemsDO;
import com.phiture.erp.produce.dal.mapper.ErpBomItemsMapper;
import com.phiture.erp.produce.dal.mapper.ErpBomMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.phiture.erp.common.constants.ErrorCodeConstants.MRP_CALCULATION_MATERIAL_BOM_NOT_EXISTS;

/**
 * @author YH
 * @date 2025-05-06-16:07
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class BomServiceImpl implements BomService {
    private final ErpBomMapper bomMapper;
    private final ErpBomItemsMapper bomItemsMapper;
    private final ErpMaterialApi materialApi;
    private final ErpMaterialPlanApi materialPlanApi;

    @Override
    public List<MrpBomInfoDTO> getBom(Long materialId) {
        ErpBomDO erpBomDO = bomMapper.selectOne(ErpBomDO::getFmaterialid, materialId);
        if (erpBomDO == null) {
            log.warn("[BomService.getBom] 未查询到BOM清单，物料ID:{}", materialId);
            return Collections.emptyList();
//            throw exception(MRP_CALCULATION_MATERIAL_BOM_NOT_EXISTS);
        }
        List<ErpBomItemsDO> items = bomItemsMapper.selectListByFparentid(erpBomDO.getFid());
        if (CollUtil.isEmpty(items)) {
            log.error("[BomService.getBom] 未查询到BOM清单，物料ID:{}", materialId);
            throw exception(MRP_CALCULATION_MATERIAL_BOM_NOT_EXISTS);
        }
        Set<Long> materialIds = items.stream().map(ErpBomItemsDO::getFmaterialid).collect(Collectors.toSet());
        Map<Long, ErpMaterialInfoResp> materialMap = materialApi.getMaterialMap(materialIds);
        Map<Long, ErpMaterialPlanResp> materialPlanMap = materialPlanApi.getMaterialPlanMap(materialIds);
        return ErpBomConvertMapper.INSTANCE.toBomInfoDTOList(items).stream()
                .peek(item -> {
                    Optional.ofNullable(materialMap.get(item.getMaterialId()))
                            .ifPresent(material -> {
                                item.setMaterialBarCode(material.getBarCode());
                                item.setMaterialName(material.getName());
                                item.setMaterialUnitId(material.getUnitId());
                                item.setMaterialUnitName(material.getUnitName());
                                item.setMaterialStandard(material.getStandard());
                            });
                    Optional.ofNullable(materialPlanMap.get(item.getMaterialId()))
                            .ifPresent(materialPlan ->{
                                item.setProduceLeadTime(materialPlan.getProduceLeadTime());
                                item.setRecevingLeadTime(materialPlan.getRecevingLeadTime());
                            });
                })
                .collect(Collectors.toList());
    }
}
